<?php

/**
 * Description of Pompier
 *
 * @author ROCU Fabien
 */
class Pompier {

    public $pompier_id;

    public $personne_id;
    public $pompier_matricule;
    public $grade_id;
    public $pompier_type;

    public $mutualisation;

    function __construct( ) // aucune information dans le constructeur
    {

    }

    public function creer_pompier(  ) // INSERT
    {
        global $_mysql;

        try {
            $query = "INSERT INTO pompier VALUES ( null, $this->personne_id, $this->pompier_matricule, $this->grade_id, '$this->pompier_type', $this->mutualisation);";
            if ( Config::$debugSQL ) echo $query;
            $result = $_mysql->query($query);

            if ( $result ) {
                $this->pompier_id = $_mysql->last_insert_id();
                return $this->pompier_id;
            } else
                throw new Exception('Erreur Pompier: la création du pompier a rencontré un problème');

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }
    }

    public function charger_pompier( $id ) // SELECT
    {
        global $_mysql;

        $this->pompier_id = $id;

        try {
            $query = "SELECT * FROM pompier WHERE pompier_id = $id";
            if ( Config::$debugSQL ) echo "<br />" . $query . "<br />";
            $result = $_mysql->query($query);

            if ( $_mysql->num_rows($result) > 0 ) {
                $val = $_mysql->fetch_array($result);

                $this->personne_id = $val['personne_id'];
                $this->pompier_matricule = $val['pompier_matricule'];
                $this->grade_id = $val['grade_id'];
                $this->pompier_type = $val['pompier_type'];
                $this->mutualisation = $val['mutualisation'];

                if ( Config::$debugWHO_IS ) echo $this->qui_suis_je();
            }
            else
                throw new Exception('Erreur Pompier: identifiant unique non respecté');

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }

    }

    public function set_pompier( $personne_id, $matricule, $grade_id, $type, $mutualisation ) {
        $this->personne_id = $personne_id;
        $this->pompier_matricule = $matricule;
        $this->grade_id = $grade_id;
        $this->pompier_type = $type;
        $this->mutualisation = $mutualisation;

        if ( Config::$debugWHO_IS ) echo $this->qui_suis_je();
    }

    public function sauvegarder_pompier() // UPDATE
    {
        global $_mysql;

        try {
            $query = "UPDATE pompier SET personne_id = " . $this->personne_id . ", pompier_matricule = " . $this->pompier_matricule . ", grade_id = " . $this->grade_id . ", pompier_type= '" . $this->pompier_type . "', mutualisation = " . $this->mutualisation . " WHERE pompier_id = " . $this->pompier_id . ";";
            if ( Config::$debugSQL ) echo $query;
            $result = $_mysql->query($query);

            if ( $result ) {

            } // Tout se passe bien ne rien faire
            else
                throw new Exception('Erreur Pompier: la modification du pompier a rencontré un problème');

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }
    }

    public function effacer_pompier( $id ) // DELETE
    {
        $pompier_id = $id;

        global $_mysql;

        try {
            $query = "DELETE FROM pompier WHERE pompier_id = $pompier_id";
            if ( Config::$debugSQL ) echo $query;
            $result = $_mysql->query($query);

            if ( $result ) {

            } // Tout se passe bien ne rien faire
            else
                throw new Exception('Erreur Pompier: la suppression du pompier a rencontré un problème');

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }
    }

    public function qui_suis_je() // Return String
    {
        return " <br /> Pompier --> ID: " . $this->pompier_id . " Personne ID: " . $this->personne_id . " Matricule: " . $this->pompier_matricule . " Grade ID: " . $this->grade_id . " Type: " . $this->pompier_type . " Mutualisation: " . $this->mutualisation . "<br />";
    }

    public function getFreePersonne() {
        global $_mysql;

        try {
            $query = "SELECT personne_id FROM personne WHERE personne_id NOT IN (SELECT personne_id FROM pompier);";
            if ( Config::$debugSQL ) echo "<br />" . $query . "<br />";
            $result = $_mysql->query($query);

            $i = 0;

            if ( $_mysql->num_rows($result) > 0 ) {
                
                while ($val = $_mysql->fetch_array($result)) {
                    $tmp[$i] = $val['personne_id'];
                    $i++;
                }

            }
            else
                throw new Exception('Erreur Pompier: getFreePersonne()');

            return $tmp;

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }
    }


}

?>
